import { BrowserRouter, Switch, Route, Redirect } from 'react-router-dom';
import Login from './pages/login';
import Address from './pages/address';
import AddressCreate from './pages/address/create';

const AuthRoute = ({ component: Comp, ...rest }) => {
    return (
        <Route
            {...rest}
            render={(props) => {
                // 去 sessionStorage 中获取登录态（登录的数据）
                const token = window.sessionStorage.getItem('token');
                // 没有 token，说明没登录，去登录
                if (!token) {
                    return <Redirect to="/login" />;
                }
                // 登录了，就原样返回
                return <Comp {...props} />;
            }}
        />
    );
};

function App() {
    return (
        <BrowserRouter>
            <Switch>
                {/* <Route path="/login" component={Login} />
                <Route exact path="/address" component={Address} />
                <Route path="/address/create" component={AddressCreate} /> */}

                <Route path="/login" component={Login} />
                <AuthRoute exact path="/address" component={Address} />
                <AuthRoute path="/address/create" component={AddressCreate} />
            </Switch>
        </BrowserRouter>
    );
}

export default App;
